#
#! /usr/bin/ruby
require 'pathname'
require 'fileutils'
require 'find'

puts "\033[1mForeground Colors…\033[0m\n"
puts " \033[30mBlack (30)\033[0m\n"
puts " \033[31\n\n\n自定义版本最大数自定义版本最大数自定义版本最大数\n\n\n自定义版本最大数自定义版本最大数自定义版本最大数 (31)\033[0m\n"
  
exit

#自定义版本最大数，
#例如最大版本是10 1.1.11要变成=> 1.2.1
#例如最大版本是20 1.20.1要变成=> 2.0.1
#$MaxVersion = 100
#
##当前路径
#File_Path = Pathname.new(__FILE__).realpath
#
##当前目录
#Cur_path = File.dirname(File_Path)
#
##正常来说当前文件所在目录名称就是项目的名称
#Project_Name = File.basename(Cur_path)
#
#Podspec_Name = "#{Project_Name}.podspec"
#
#$Project_Tag = ''

# class Tag
#     # 修改之后的版本号
#     Project_Tag = ''

    # def editTag

    #     specFile = File.new(Podspec_Name,"r+")
 
    #     if specFile
    #         content = ""
    #         specFile.each_line do |line|

    #             tmpLine = line
    #             tmpLine = tmpLine.gsub(' ','')
    #             if tmpLine.include?("s.version=")

    #                 tmpLine = tmpLine.gsub('"','')
    #                 tmpLine = tmpLine.gsub('\'','')
    #                 tmpLine = tmpLine.gsub('s.version=','')

    #                 list = tmpLine.split('.')
    #                 one = list[0].to_i
    #                 two = list[1].to_i
    #                 three = list[2].to_i + 1

    #                 #计算版本
    #                 if three >= $MaxVersion
    #                     three = three % $MaxVersion;
    #                     two = two + 1
    #                     if two >= $MaxVersion
    #                         two = two % $MaxVersion;
    #                         one = one + 1
    #                     end
    #                 end
    #                 #版本号
    #                 tag  = "#{one}.#{two}.#{three}"
    #                 # 重新生成字符串
    #                 line = "  s.version          = '#{tag}'\n"
    #                 #记录版本号 git tag 使用
    #                 Project_Tag.replace tag.strip
    #             end
    #             content+=line
    #         end
    #         File.open(Podspec_Name,"w"){|l| #以写的方式打开文件，将buffer覆盖写入文件
    #             l.write(content)
    #         }
    #     else
    #     puts ">>ERROR:没找到spec文件！！"
    #     end

    # end
# end

#def editTag
#
#    specFile = File.new(Podspec_Name,"r+")
#
#    if specFile
#        content = ""
#        tag = ''
#        specFile.each_line do |line|
#            tmpLine = line
#            tmpLine = tmpLine.gsub(' ','')
#            if tmpLine.include?("s.version=")
#
#                tmpLine = tmpLine.gsub('"','')
#                tmpLine = tmpLine.gsub('\'','')
#                tmpLine = tmpLine.gsub('s.version=','')
#
#                list = tmpLine.split('.')
#                one = list[0].to_i
#                two = list[1].to_i
#                three = list[2].to_i + 1
#
#                #计算版本
#                if three >= $MaxVersion
#                    three = three % $MaxVersion;
#                    two = two + 1
#                    if two >= $MaxVersion
#                        two = two % $MaxVersion;
#                        one = one + 1
#                    end
#                end
#                #版本号
#                tag  = "#{one}.#{two}.#{three}"
#                # 重新生成字符串
#                line = "  s.version          = '#{tag}'\n"
#            end
#            content+=line
#        end
#        File.open(Podspec_Name,"w"){|l| #以写的方式打开文件，将buffer覆盖写入文件
#            l.write(content)
#        }
#        return tag
#    else
#        puts ">>ERROR:没找到spec文件！！"
#    end
#
#end
#
#$Project_Tag = editTag
#puts $Project_Tag
#$Project_Tag = editTag
#puts $Project_Tag
#$Project_Tag = editTag
#puts $Project_Tag
#

# def pushCode

#     system "git add ."
#     if ARGV.first.length > 0
#         system "git commit -m '#{ARGV.first}'"
#     else
#         system "git commit -m '更新版本号#{$Project_Tag}'"
#     end
#     system "git push -u origin master"  
#     system "git tag #{$Project_Tag}"
#     system "git push --tags"
# end




# def buildframework

#     if File::exist?(Cur_path + '/pod_ci.rb')
#         puts "开始编译buildframework"
#         system "ruby pushTag.rb"
#         pushCode
#     end

# end


# def buildframework

#     if File::exist?(Cur_path + '/pod_ci.rb')
#         puts "开始编译buildframework"
#         system "ruby pushTag.rb"
#         pushCode
#     end

# end


# def pushRepo

#     output = system "pod lib lint --allow-warnings --verbose --sources=https://gitee.com/chengssir/MMSpecs.git,https://github.com/CocoaPods/Specs.git --no-clean --skip-import-validation"

#     if output == true
#         puts "   \033[41m 本地校验成功 \033[0m\n"
#         system "pod repo push gitee-chengssir-mmspecs #{$podspecName} --allow-warnings --verbose --sources=https://gitee.com/chengssir/MMSpecs.git,https://github.com/CocoaPods/Specs.git"
#     else
#         puts "   \033[41m 本地校验失败 \033[0m\n"
#     end

# end


# pushCode
# buildframework
# pushRepo
# 
# puts addTag





#$MaxVersion = 100
#
#class Color
#    def self.natural
#        0
#    end
#    def self.red
#        31
#    end
#    def self.green
#        32
#    end
#    def self.white
#        37
#    end
#end
#
#def color_text(text, color = Color.natural)
#    if color == 0
#        return text
#    end
#    return "\033[#{color}m#{text}\033[0m"
#end
#
#def die_log(text)
#    puts color_text(text, Color.red)
#end
#
##文件路径
#cur_path = Dir.pwd
#
##podspec的名字，可能存在多个podspec
#push_podspec_name = ''
#
##远程库名字
#pod_repo_name = 'MMSpecs'
#
##远程Specs地址
#pod_repo_source = 'https://gitee.com/chengssir/MMSpecs.git'
#
##是否为静态库
#is_static_lib = false
#
#
#user_custom_version = false
## 检查是否存在 SpecPushFile 文件，如果不存在，那么创建
#if not File::exist?(cur_path + '/ZPodPushFile')
#    system('touch ZPodPushFile')
#    File.open(cur_path + '/ZPodPushFile', 'w+') do |f|
#        f.write("
##请吧ZPodPushFile.rb放在*.podspec同级目录下
#
##用户还可以指定要推送的podspec文件的名字，这个可能存在多个podspec
#PODSPEC_NAME=
#
##pod repo库的名字，请看~/.cocoapods/repos目录
#POD_REPO_NAME=trunk
#
##pod repo的源地址
#POD_REPO_SOURCE=https://github.com/CocoaPods/Specs
#
##如果这个库是静态库，那么需要设置为true
#POD_IS_STATIC_LIBRARY=false")
#    end
#    puts color_text('Create ZPodPushFile', Color.green)
#    puts color_text("First you should modify 'PodPushFile' file and run the script again", Color.white)
#    system('open ZPodPushFile')
#    return
#end
#
#puts color_text('Parse ZPodPushFile...', Color.white)
#
##从文件中取值
#File.open(cur_path + '/ZPodPushFile') do |f|
#    f.each_line do |line|
#        key_value = line.split('=')
#        if key_value.count == 1 then
#            next
#        end
#        key = key_value.first.to_s.gsub("\n", '').gsub(' ','').gsub("\t",'')
#        value = key_value.last.to_s.gsub("\n", '').gsub(' ','').gsub("\t",'')
#
#        if key.to_s == 'PODSPEC_NAME' and not value.nil?
#            push_podspec_name = value.to_s
#        elsif key.to_s == 'POD_REPO_NAME' and not value.nil?
#            pod_repo_name = value.to_s
#        elsif key.to_s == 'POD_REPO_SOURCE' and not value.nil?
#            pod_repo_source = value
#        elsif key.to_s == 'POD_IS_STATIC_LIBRARY' and not value.nil?
#            is_static_lib = value == 'true'
#        end
#        # puts "key=#{key},value=#{value}"
#
#    end
#end
#
#puts "Push path is: #{cur_path} push_podspec_name is #{push_podspec_name}"
###
### 搜索podspec路径
#podspec_path = ''
#find_podspec_reg = ''
#
#if push_podspec_name.length > 0
#   # 用户指定要推送某个podspec
#   if push_podspec_name.include?('.podspec')
#       find_podspec_reg += push_podspec_name
#
#   else
#       find_podspec_reg += (push_podspec_name + '.podspec')
#   end
#else
#   find_podspec_reg += '*.podspec'
#end
#puts "Find podspec reg = #{find_podspec_reg}"
#
## 有可能存在多个 podspec，当用户没有指定时，需要给用户自主选择
#find_podspec_count = 0
#podspecs = Array.new
#Dir::glob(find_podspec_reg) do |f|
#   find_podspec_count += 1
#   podspecs << f
#end
#
#if podspecs.count > 1
#   inputTag = true
#   serial = 0
#   puts color_text("Find #{podspecs.count} podspec files, please enter the serial number selection:",Color.white)
#   while inputTag
#       for i in 0...podspecs.count do
#           puts "#{i+1}. #{podspecs[i]}"
#       end
#       serial = gets.chomp
##       inputTag = (serial.to_i > podspecs.count || serial.to_i <= 0)
##       if inputTag
#           puts "Input serial = #{serial}, it's invalid and you need to input 1~#{podspecs.count}:"
##       end
#   end
#   podspec_path = podspecs[serial.to_i-1]
#elsif podspecs.count == 1
#   podspec_path = podspecs[0]
#else
#   puts color_text("Can't find any podspec file", Color.red)
#   return
#end
#
#if not File::exist?(podspec_path)
#   die_log("Can't find any podspec file in path: #{podspec_path}, please modify PodPushFile' PUSH_DIR_PATH(key)")
#   return
#else
#   puts "Ready to deal with podspec named " + color_text("#{podspec_path}", Color.white)
#end
##
## 原podspec路径
#podspec_absolute_path = cur_path + '/' + podspec_path
#
#cur_version = ''
#new_version = ''
#
### 读取当前podspec文件的版本
#File.open(podspec_absolute_path, 'r+') do |f|
#   f.each_line do |line|
#       # 查找.version
#       version_desc = /.*\.version[\s]*=.*/.match line
#       if not version_desc.nil?
#           cur_version = version_desc.to_s.split('=').last.to_s.gsub("'", '')
#           cur_version = cur_version.gsub(' ', '')
#           break
#       end
#   end
#end
#puts color_text("Current version = ", Color.white) + color_text("#{cur_version}", Color.green)
#
#new_version = ''
#git_source = ''
#File.open(podspec_absolute_path, 'r+') do |t|
#   File.open(podspec_absolute_path) do |f|
#       f.each_line do |line|
#           # # 查找.version
#           # s.version      = "0.0.2"
#           # 需要注意的是，版本号可以是''，也可以是""
#           write_line = line
#           version_desc = /.*\.version[\s]*=.*/.match line
#           if not version_desc.nil?
#                version_coms = version_desc.to_s.split('=')
#                version_num = version_coms.last.to_s.gsub("'",'').gsub("\"",'').gsub(' ','')
#                v_s = version_num.split('.')
#                one = v_s[0].to_i
#                two = v_s[1].to_i
#                three = v_s[2].to_i + 1
#
#                #计算版本
#                if three >= $MaxVersion
#                    three = three % $MaxVersion;
#                    two = two + 1
#                    if two >= $MaxVersion
#                        two = two % $MaxVersion;
#                        one = one + 1
#                    end
#                end
#                new_version  = "#{one}.#{two}.#{three}"
#
#                puts color_text("New version = ",Color.white) + color_text("#{new_version}", Color.green)
#                write_line = version_coms.first.to_s + '=' + " '#{new_version}'" + "\n"
#           end
#           source_desc = /.*\.source[\s]*=.*/.match line
#           if not source_desc.nil?
#               source_desc = /:git.*,/.match source_desc.to_s
#               source_desc = /'.*'/.match source_desc.to_s
#               git_source = source_desc.to_s.gsub("'",'')
#               puts "git source is #{git_source}"
#           end
#           t.write write_line
#       end
#   end
#end
#
#puts color_text("Update version from ",Color.white) + color_text("#{cur_version}",Color.green) + color_text(" to ",Color.white) + color_text("#{new_version}", Color.green)
#
#
## 如果本地没有这个repo，那么添加
#if system("pod repo | grep #{pod_repo_name}") == false
#   puts color_text("Add pod repo named '#{pod_repo_name}' with source: #{pod_repo_source}", Color.white)
#   system("pod repo add #{pod_repo_name} #{pod_repo_source}")
#end
#
## 提交代码到远程仓库
#puts color_text('Start upload code to remote', Color.white)
#system("git commit -am 'update version to #{new_version}'")
#if system('git push origin') == false
#   die_log('[!] git push code error')
#end
#system("git tag #{new_version}")
#if system('git push origin --tags') == false
#   die_log('[!] git push tags error')
#   return
#end
#
## 验证podspec格式是否正确
#if verify_podspec_format == true
#   puts color_text("Start verify podspec '#{podspec_path}'...", Color.white)
#   if system("pod lib lint #{podspec_path} --allow-warnings") == false
#       die_log("[!] pod spec' format invalid")
#       return
#   end
#end
##
## 提交pod spec到spec仓库
#puts color_text("Start push pod '#{podspec_path}' to remote repo '#{pod_repo_name}'", Color.white)
#if pod_repo_name == 'trunk'
#   if (is_static_lib == true ? system("pod trunk push #{podspec_path} --allow-warnings --use-libraries") : system("pod trunk push #{podspec_path} --allow-warnings")) == false
#       puts "If not timeout, you need to check your 'trunk' account like: 'pod trunk me', and register code is 'pod trunk register <your email> <your name>'"
#       return
#   end
#else
#   if (is_static_lib == true ? system("pod repo push #{pod_repo_name} #{podspec_path} --allow-warnings --use-libraries") : system("pod repo push #{pod_repo_name} #{podspec_path} --allow-warnings"))  == false
#       return
#   end
#end
#puts color_text("Update success ☕️! Current version = #{new_version}", Color.green)
